//==============================================================================
//              Cooperative MultiTasking system
//                    CMT system
// By Alexander Sibilev
//==============================================================================

#define WBVAL(x) (x & 0xFF),((x >> 8) & 0xFF)

/* USB Standard Device Descriptor */
const uint8 Virtual_Com_Port_DeviceDescriptor[] =
  {
    0x12,   /* bLength */
    USB_DEVICE_DESCRIPTOR_TYPE,     /* bDescriptorType */
    0x00,
    0x02,   /* bcdUSB = 2.00 */
    0x02,   /* bDeviceClass: CDC */
    0x00,   /* bDeviceSubClass */
    0x00,   /* bDeviceProtocol */
    0x40,   /* bMaxPacketSize0 */
    0x83,
    0x04,   /* idVendor = 0x0483 */
    0x40,
    0x57,   /* idProduct = 0x7540 */
    0x00,
    0x02,   /* bcdDevice = 2.00 */
    1,              /* Index of string descriptor describing manufacturer */
    2,              /* Index of string descriptor describing product */
    3,              /* Index of string descriptor describing the device's serial number */
    0x01    /* bNumConfigurations */
  };

#define VIRTUAL_COM_PORT_SIZ_CONFIG_DESC        67

const uint8 Virtual_Com_Port_ConfigDescriptor[] =
  {
    /*Configuration Descriptor*/
    0x09,   /* bLength: Configuration Descriptor size */
    USB_CONFIGURATION_DESCRIPTOR_TYPE,      /* bDescriptorType: Configuration */
    VIRTUAL_COM_PORT_SIZ_CONFIG_DESC,       /* wTotalLength:no of returned bytes */
    0x00,
    0x02,   /* bNumInterfaces: 2 interface */
    0x01,   /* bConfigurationValue: Configuration value */
    0x00,   /* iConfiguration: Index of string descriptor describing the configuration */
    0xC0,   /* bmAttributes: self powered */
    0x32,   /* MaxPower 0 mA */
    /*Interface Descriptor*/
    0x09,   /* bLength: Interface Descriptor size */
    USB_INTERFACE_DESCRIPTOR_TYPE,  /* bDescriptorType: Interface */
    /* Interface descriptor type */
    0x00,   /* bInterfaceNumber: Number of Interface */
    0x00,   /* bAlternateSetting: Alternate setting */
    0x01,   /* bNumEndpoints: One endpoints used */
    0x02,   /* bInterfaceClass: Communication Interface Class */
    0x02,   /* bInterfaceSubClass: Abstract Control Model */
    0x01,   /* bInterfaceProtocol: Common AT commands */
    0x00,   /* iInterface: */
    /*Header Functional Descriptor*/
    0x05,   /* bLength: Endpoint Descriptor size */
    0x24,   /* bDescriptorType: CS_INTERFACE */
    0x00,   /* bDescriptorSubtype: Header Func Desc */
    0x10,   /* bcdCDC: spec release number */
    0x01,
    /*Call Management Functional Descriptor*/
    0x05,   /* bFunctionLength */
    0x24,   /* bDescriptorType: CS_INTERFACE */
    0x01,   /* bDescriptorSubtype: Call Management Func Desc */
    0x00,   /* bmCapabilities: D0+D1 */
    0x01,   /* bDataInterface: 1 */
    /*ACM Functional Descriptor*/
    0x04,   /* bFunctionLength */
    0x24,   /* bDescriptorType: CS_INTERFACE */
    0x02,   /* bDescriptorSubtype: Abstract Control Management desc */
    0x02,   /* bmCapabilities */
    /*Union Functional Descriptor*/
    0x05,   /* bFunctionLength */
    0x24,   /* bDescriptorType: CS_INTERFACE */
    0x06,   /* bDescriptorSubtype: Union func desc */
    0x00,   /* bMasterInterface: Communication class interface */
    0x01,   /* bSlaveInterface0: Data Class Interface */
    /*Endpoint 2 Descriptor*/
    0x07,   /* bLength: Endpoint Descriptor size */
    USB_ENDPOINT_DESCRIPTOR_TYPE,   /* bDescriptorType: Endpoint */
    0x82,   /* bEndpointAddress: (IN2) */
    0x03,   /* bmAttributes: Interrupt */
    8,      /* wMaxPacketSize: */
    0x00,
    0xFF,   /* bInterval: */
    /*Data class interface descriptor*/
    0x09,   /* bLength: Interface Descriptor size */
    USB_INTERFACE_DESCRIPTOR_TYPE,  /* bDescriptorType: */
    0x01,   /* bInterfaceNumber: Number of Interface */
    0x00,   /* bAlternateSetting: Alternate setting */
    0x02,   /* bNumEndpoints: Two endpoints used */
    0x0A,   /* bInterfaceClass: CDC */
    0x00,   /* bInterfaceSubClass: */
    0x00,   /* bInterfaceProtocol: */
    0x00,   /* iInterface: */
    /*Endpoint 3 Descriptor*/
    0x07,   /* bLength: Endpoint Descriptor size */
    USB_ENDPOINT_DESCRIPTOR_TYPE,   /* bDescriptorType: Endpoint */
    0x03,   /* bEndpointAddress: (OUT3) */
    0x02,   /* bmAttributes: Bulk */
    64,             /* wMaxPacketSize: */
    0x00,
    0x00,   /* bInterval: ignore for Bulk transfer */
    /*Endpoint 1 Descriptor*/
    0x07,   /* bLength: Endpoint Descriptor size */
    USB_ENDPOINT_DESCRIPTOR_TYPE,   /* bDescriptorType: Endpoint */
    0x81,   /* bEndpointAddress: (IN1) */
    0x02,   /* bmAttributes: Bulk */
    64,             /* wMaxPacketSize: */
    0x00,
    0x00,    /* bInterval */
    0x00     //stop
  };

/* USB String Descriptors */
const uint8 Virtual_Com_Port_StringLangID[4] =
  {
    4,
    USB_STRING_DESCRIPTOR_TYPE,
    0x09,
    0x04 /* LangID = 0x0409: U.S. English */
  };

const uint8 Virtual_Com_Port_StringVendor[38] =
  {
    38,     /* Size of Vendor string */
    USB_STRING_DESCRIPTOR_TYPE,             /* bDescriptorType*/
    /* Manufacturer: "STMicroelectronics" */
    'R', 0, 'D', 0, 'M', 0, '-', 0, 'C', 0, 'o', 0, 'n', 0, 't', 0,
    'a', 0, 'c', 0, 't', 0, '.', 0, 'S', 0, 'i', 0, 'b', 0, 'l', 0,
    'e', 0, 'v', 0
  };

const uint8 Virtual_Com_Port_StringProduct[50] =
  {
    50,          /* bLength */
    USB_STRING_DESCRIPTOR_TYPE,        /* bDescriptorType */
    /* Product name: "STM32 Virtual COM Port" */
    'R', 0, 'D', 0, 'M', 0, '-', 0, 'C', 0, ' ', 0, 'V', 0, 'i', 0,
    'r', 0, 't', 0, 'u', 0, 'a', 0, 'l', 0, ' ', 0, 'C', 0, 'O', 0,
    'M', 0, ' ', 0, 'P', 0, 'o', 0, 'r', 0, 't', 0, ' ', 0, ' ', 0
  };

uint8 Virtual_Com_Port_StringSerial[26] =
  {
    26,           /* bLength */
    USB_STRING_DESCRIPTOR_TYPE,                   /* bDescriptorType */
    'S', 0, 'T', 0, 'M', 0, '3', 0, '2', 0, '1', 0, '1', 0
  };

void
CmtUsbDevCom::EndPoint0OutComplete() {
  if( mClassDataCode == SET_LINE_CODING ) {
    mClassDataCode = 0;
    SetupComParam();
    }
  USB_StatusInStage();
  }


uint_8 
CmtUsbDevCom::RequestClass() {
  static int firstEp = 0;
  switch (SetupPacket.bRequest) {
    // set line coding
    case SET_LINE_CODING:
      mClassDataCode = SET_LINE_CODING;
      USB_DataOut( &mLineCoding, SetupPacket.wLength );
      break;

    // get line coding
    case GET_LINE_CODING:
      pData  = (uint8*)(&mLineCoding);
      Count = 7;
      USB_DataInStage();
      if( firstEp == 0 ) {
        firstEp = 1;
        USB_EnableEP( 0x03 );
        USB_EnableEP( 0x81 );
        USB_EnableEP( 0x82 );
        }
      break;

    // set control line state
    case SET_CONTROL_LINE_STATE:
      // bit0 = DTR, bit = RTS
      USB_StatusInStage();
      break;
    default : return CMTE_FAIL;
    }
  return CMTE_OK;
  
  }

uint8* 
CmtUsbDevCom::GetDeviceDescriptor() {
  return (uint8*)Virtual_Com_Port_DeviceDescriptor;
  }

uint8* 
CmtUsbDevCom::GetConfigDescriptor( uint_8 ) {
  return (uint8*)Virtual_Com_Port_ConfigDescriptor;
  }

uint8* 
CmtUsbDevCom::GetStringDescriptor( uint_8 stringIndex ) {
  switch( stringIndex ) {
    case 1 : return (uint8*)Virtual_Com_Port_StringVendor;
    case 2 : return (uint8*)Virtual_Com_Port_StringProduct;
    case 3 : return (uint8*)Virtual_Com_Port_StringSerial;
    }
  return (uint8*)Virtual_Com_Port_StringLangID;
  }

CmtUsbDevCom::CmtUsbDevCom() :
  CmtUsbDev(),
  mClassDataCode(0) {
  mLineCoding.dwDTERate   = 9600;
  mLineCoding.bCharFormat = 0;
  mLineCoding.bParityType = 0;
  mLineCoding.bDataBits   = 8;
  }
  
